<?php
/*
 * Created on 08.01.2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 * 
 * attributes:
 *  returnKeyCallback: Callback javascript function
 *  description:       Description for the form
 */

m2import('phpinchains/psp/Tag.php');

class xt_searchFormHandler extends phpinchains_psp_Tag
{

    /**
     * Is a body tag (if false, body is ignored)
     */
    var $tagType = PSP_BODY_TAG;
    /**
     * Tag name in form namespace:tagname
     */
    var $tagName   = 'xt:searchForm';
    var $name = "";
    var $var = "";

    function startElement(phpinchains_parser_psp_PSPParser $sender, $attrs, $line, $file = '') {
        $attrs['var'] = (empty($attrs['var']))?"''":$attrs['var'];
        $attrs['description'] = (empty($attrs['description']))?"''":$attrs['description'];
        $attrs['returnKeyCallback'] = (empty($attrs['returnKeyCallback']))?"''":$attrs['returnKeyCallback'];
                
        $this->var = (empty($attrs['var'])?'formdata':substr($attrs['var'], 1, -1));
        parent::startElement($sender, $attrs, $line, $file);
        $sender->surpressCDataAppend(true);
        if ($attrs['loadValue'])
        {
            $sender->appendCode('$'."__url = new phpinchains_tagsupport_URL(\$env, ".$attrs['loadValue'].");\n");
        }
        else
        {
            $sender->appendCode('$'."__url = new phpinchains_tagsupport_URL(\$env);\n");
        }
        if ($attrs['loadAction'])
        {
            $sender->appendCode('$'."__url->setAction(".$attrs['loadAction'].");\n");
        }
        if ($attrs['loadModule'])
        {
            $sender->appendCode('$'."__url->setModule(".$attrs['loadModule'].");\n");
        }
        $sender->appendCode('$__'."url->addParam('jsresponse' , 'true');\n");
       	$sender->appendCode("\$run = &phpinchains_RuntimeConfig::getInstance();\n");
       	$sender->appendCode("if (\$run->get('sec_code_enabled')) {\n");
       	$sender->appendCode('    $'."__url->addParam('__ss', \$env->getSession('phpinchains_sec_code'));\n");
       	$sender->appendCode("}\n");
       	$sender->appendCode("if (\$run->get('additional_parameters')) {\n");
       	$sender->appendCode('    $'."__url->addParams(\$run->get('additional_parameters'));\n");
       	$sender->appendCode("}\n");
        $mode = ", mode => ".($attrs['mode']?$attrs['mode']:'normal');
        $sender->appendRequestableDataCode("'xt:searchForm_" . substr($attrs['var'], 1, -1) . "' => array('description' => ".$attrs['description']."$mode, 'loadAction' => ".$attrs['loadAction'].", 'returnKeyCallback' => ".$attrs['returnKeyCallback'].", 'types' => array(");
        $sender->appendCode("\$_xtSearchForm = array('description' => ".$attrs['description']."$mode, 'loadAction' => \$__url->toString(), 'returnKeyCallback' => ".$attrs['returnKeyCallback'].", 'types' => array(");
    }

    function endElement(&$sender) {
        $sender->appendRequestableDataCode(")),\n");
        $sender->appendCode("));{$this->varname}->setAttribute('struct', \$_xtSearchForm);\n");
        parent::endElement($sender);
        $sender->surpressCDataAppend(false);
    }


    function doStartTag()
    {
        // initializes $this->buffer and pushes the listener on the stack of the 
        // main output writer
        return PSP_EVAL_BODY_BUFFERED;
    }

    function doEndTag()
    {
        $struct = $this->getAttribute('struct');
        if (is_array($struct))
        m2import("Zend/Json.php");
        $encoding = 'utf-8';
        if ($encoding != 'utf-8') {
        	Zend_Json::$useBuiltinEncoderDecoder = true;
        }
        $enc = Zend_Json::encode($struct);
        $this->out->write('<script type="text/javascript">'."\n var ".$this->getAttribute('var').' = '.$enc."\n".'; var '.$this->getAttribute('var').'_sf = new phpinchains.SearchForm("'.$this->getAttribute('var').'", '.$this->getAttribute('var').');'."\n".'</script>');
        return PSP_SKIP_BODY;
    }

}
